﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Windows.Threading;
using Microsoft.Windows.Controls;

namespace UTC_Project_Manager.Controls
{
    /// <summary>
    /// Interaction logic for CalendarControl.xaml
    /// </summary>
    public partial class CalendarControl : UserControl
    {
        static Dictionary<DateTime, string> dict = new Dictionary<DateTime, string>();

        public CalendarControl()
        {
            InitializeComponent();
            ProjectSettingsControl.newDateEvent += new ProjectSettingsControl.PassNewDate(SettingsControl_newDateEvent);
            NewReunionControl.newReunionCreatedEvent += new NewReunionControl.NewReunionEvent(NewReunionControl_newReunionCreatedEvent);
            ReunionListControl.deleteReunionEvent += new ReunionListControl.DeleteReunionEvent(ReunionListControl_deleteReunionEvent);
        }

        void ReunionListControl_deleteReunionEvent(UTC_Project_Manager.Model.Infrastructure.Meeting meeting)
        {
            DateTime date = new DateTime(meeting.Date.Year, meeting.Date.Month, meeting.Date.Day);
            try
            {
                if (App.MyConfig.dict.ContainsKey(date))
                {
                    String desc = App.MyConfig.dict[date];
                    int lines = 0;
                    foreach (char c in desc) 
                    {
                        if (c == '\n') lines++;
                    }
                    if (lines == 0)
                        App.MyConfig.dict.Remove(date);
                    else 
                    {
                        String oldDesc = "[" + meeting.Date.TimeOfDay + "] " + meeting.Title;
                        desc.Replace(oldDesc, "");

                    }
                }
            }
            catch (ArgumentException) { }
        }

        void NewReunionControl_newReunionCreatedEvent(UTC_Project_Manager.Model.Infrastructure.Meeting meeting)
        {
            DateTime date = new DateTime(meeting.Date.Year, meeting.Date.Month, meeting.Date.Day);
            try
            {
                if (App.MyConfig.dict.ContainsKey(date))
                {
                    App.MyConfig.dict[date] += "\n" + "[" + meeting.Date.TimeOfDay + "] " + meeting.Title;
                }
                else App.MyConfig.dict.Add(date, "[" + meeting.Date.TimeOfDay + "] " + meeting.Title);
            }
            catch (ArgumentException) { }
        }

        // TODO: Need to refresh the calendar to reflect the date change
        void SettingsControl_newDateEvent(DateTime oldDate, DateTime newDate, String description)
        {
            App.MyConfig.dict.Remove(oldDate);
            if (description == "StartDate")
                App.MyConfig.dict.Add(newDate, "Début du projet");
            else if (description == "EndDate")
                App.MyConfig.dict.Add(newDate, "Fin du projet");

            //Refresh calendar... but, how? >.<
            this.InitializeComponent();
            this.Refresh();

           
        }

    }

    public static class ExtensionMethods
    {
        private static Action EmptyDelegate = delegate() { };

        public static void Refresh(this UIElement uiElement)
        {
            uiElement.Dispatcher.Invoke(DispatcherPriority.Render, EmptyDelegate);
        }
    }
}
